package org.hang.gift.listener;

import com.alibaba.fastjson.JSONArray;
import org.apache.rocketmq.common.message.MessageExt;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.hang.gift.constants.GiftConstants;
import org.hang.gift.doc.GiftDoc;
import org.hang.gift.repository.GiftDocRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 监听消息队列
 * 用来将下架的礼物信息从es中删除
 */
@Component
@RocketMQMessageListener(topic = GiftConstants.SPU_OFFSALE_TX_TOPIC,consumerGroup = GiftConstants.SPU_OFFSALE_TX_CONSUMER_GROUP)
public class GoodsSpuOffsaleMessageListener implements RocketMQListener<MessageExt> {

    @Autowired
    private GiftDocRepository giftDocRepository;

    @Override
    public void onMessage(MessageExt message) {
        // 1 获取消息队列中的消息
        byte[] body = message.getBody();
        //转成json字符串格式
        String idstr = new String(body);
        List<Long> ids = JSONArray.parseArray(idstr, Long.class);
        // 2 将商品信息从es中删除
        for (Long id:ids) {
            giftDocRepository.deleteById(id);
        }
        //giftDocRepository.deleteAll(ids);
    }
}
